<?php
define( "DB_SERVER",    getenv('OPENSHIFT_MYSQL_DB_HOST') );
define( "DB_USER",      getenv('OPENSHIFT_MYSQL_DB_USERNAME') );
define( "DB_PASSWORD",  getenv('OPENSHIFT_MYSQL_DB_PASSWORD') );
define( "DB_DATABASE",  getenv('OPENSHIFT_APP_NAME') );

$database = DB_DATABASE;

mysql_connect(DB_SERVER,DB_USER,DB_PASSWORD) or die('cant connect to database');
mysql_select_db(DB_DATABASE) or die("Unable to select database");

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if ($putdata = fopen("php://input", "rb"))
    {
        $json = "";
        while ($data = fread($putdata, 1024))
        {
            $json .= $data;
        }
        fclose($putdata);

    } else
    {
        echo '<record>';
        header("HTTP/1.1 500 Internal Server Error");
        echo "** Error opening received file<br>\n";
        echo '</record>';
        return;
    }

	$obj = json_decode($json);
    $query = "INSERT INTO gcm VALUES ( '" . $obj->{'androidId'} . "','" . $obj->{'regId'} . "') ON DUPLICATE KEY UPDATE regid = VALUES( regid ); " ;
    $result = mysql_query($query) or die("Error");

}
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $query = "SELECT userid FROM gcm ; " ;
    $result = mysql_query($query) or die("Error");
    if( !$result) die("Error");
	echo "Registered devices:<br>\n\n";
    while ($row = mysql_fetch_assoc($result)) {
		echo $row['userid'] . "<br>\n";
	}
} 

?>